package com.czk.zookeeper.zookeepercli.watcher.children;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/1/4 21:06
 */
@SuppressWarnings("ALL")
public class ZkWatcherGetChildTest {
    String IP = "192.168.25.133:2181";
    ZooKeeper zooKeeper = null;

    @Before
    public void before() throws Exception {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        zooKeeper = new ZooKeeper(IP, 100000, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("连接成功");
                if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
                    // 连接成功
                    countDownLatch.countDown();
                }
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        // 连接是异步的，此时应该阻塞
        countDownLatch.await();
    }

    @After
    public void after() throws Exception {
        zooKeeper.close();
    }

    /**
     * 使用连接对象的watcher
     * 监听子节点
     * 可以捕获子节点发生变化与当前节点被删除
     *
     * @throws Exception
     */
    @Test
    public void test() throws Exception {
        // arg1 节点路径
        // arg2 使用连接对象中的watcher
        zooKeeper.getChildren("/watcher", true);
        Thread.sleep(10 * 1000);
        System.out.println("结束");
    }

    /**
     * 自定义watcher
     *
     * @throws Exception
     */
    @Test
    public void test1() throws Exception {
        // arg1 节点路径
        // arg2 自定义watcher
        zooKeeper.getChildren("/watcher", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        Thread.sleep(10 * 1000);
        System.out.println("结束");
    }

    @Test
    public void test2() throws Exception {
        // arg1 节点路径
        // arg2 自定义watcher
        zooKeeper.getChildren("/watcher", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
                // 多次注册，判断原因是方式子节点被删除后再注册
                if (watchedEvent.getType() == Event.EventType.NodeChildrenChanged) {
                    // 进行注册
                    try {
                        zooKeeper.getChildren("/watcher", this);
                    } catch (KeeperException | InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        Thread.sleep(10 * 1000);
        System.out.println("结束");
    }
}
